package com.atguigu.gmall.order.rpc;

import com.atguigu.gmall.common.config.utils.UserAuthUtil;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.order.entity.OrderInfo;
import com.atguigu.gmall.order.service.OrderBizService;
import com.atguigu.gmall.order.vo.OrderConfirmVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description
 * @Return
 * @Author https://weiranyi.github.io
 * @Date Created in 2022-12-21 09:01
 **/
@RequestMapping("/api/inner/rpc/order")
@RestController
public class OrderRpcController {
    @Autowired
    OrderBizService orderBizService;

    @GetMapping("/info/{orderId}")
    public Result<OrderInfo> getOrderInfo(@PathVariable("orderId") Long orderId) {
        String userId = UserAuthUtil.getUserIdHeader();
        if (userId == null){
            // 防止越权风险
            return null;
        }
        OrderInfo orderInfo = orderBizService.getOrderInfo(userId, orderId);
        return Result.ok(orderInfo);
    }

    /**
     * 返回订单确认页数据
     *
     * @return
     */
    @RequestMapping("/confirmData")
    public Result<OrderConfirmVo> getOrderConfirmData() {
        OrderConfirmVo orderConfirmVo = orderBizService.getOrderConfirmData();
        return Result.ok(orderConfirmVo);
    }

    @PostMapping("/submitOrder")
    public Result<Long> saveOrder(@RequestBody OrderInfo orderInfo){
        Long id = orderBizService.insertSeckillOrder(orderInfo);
        return Result.ok(id);
    }
}
